#include <linux/module.h>
#include <linux/fs.h>
#include <linux/cdev.h>
#include <linux/io.h>
#include <linux/errno.h>
#include <linux/uaccess.h>
#include <linux/platform_device.h>
#include <linux/device.h>
#include <linux/of.h>
#include <linux/pinctrl/consumer.h>
#include <linux/pinctrl/machine.h>
#include <linux/pinctrl/devinfo.h>
#include <linux/pinctrl/pinctrl.h>
#include <linux/list.h>
#include <linux/printk.h>
#include <linux/interrupt.h>
#include <linux/of_irq.h>
#include <linux/of_gpio.h>
#include <linux/gpio/consumer.h>
#include <linux/spinlock.h>
#include <linux/i2c.h>
#include <linux/err.h>
#include <linux/regmap.h>
#include <linux/slab.h>
#include <asm/uaccess.h>
#include <linux/delay.h>
#include <linux/init.h>
#include <linux/ioctl.h>
#include <linux/mutex.h>
#include <linux/slab.h>
#include <linux/compat.h>
#include <linux/of_device.h>
#include <linux/acpi.h>
#include <linux/spi/spi.h>
#include <linux/spi/spidev.h>
#include <linux/usb.h>
#include <linux/miscdevice.h>
#include <linux/sched.h>
#include <linux/wait.h>
#include <linux/hid.h>
#include <media/v4l2-device.h>
#include <media/v4l2-ioctl.h>
#include <media/v4l2-ctrls.h>
#include <media/v4l2-event.h>
#include <media/videobuf2-v4l2.h>
#include <media/videobuf2-vmalloc.h>
#include <linux/irq.h>
#include <linux/interrupt.h>
#include <video/display_timing.h>
#include <linux/clk.h>
#include <linux/dma-mapping.h>
#include <linux/fb.h>
#include <video/of_display_timing.h>
#include <video/of_videomode.h>
#include <video/videomode.h>

#define     LCD_WIDTH                  /* LCD面板的行宽 */
#define     LCD_HEIGHT                 /* LCD面板的列宽 */

#define     VSPW                       /* 通过计算无效行数垂直同步脉冲宽度决定VSYNC脉冲的高电平宽度 */
#define     VBPD                       /* 垂直同步周期后的无效行数 */
#define     LINEVAL     (LCD_HEIGHT-1) /* LCD的垂直宽度-1 */
#define     VFPD                       /* 垂直同步周期前的的无效行数 */

#define     CLKVAL                     /* VCLK = HCLK / [(CLKVAL  + 1)  × 2] */

#define     HSPW                       /* 通过计算VCLK的数水平同步脉冲宽度决定HSYNC脉冲的高电平宽度 */
#define     HBPD                       /* 描述水平后沿为HSYNC的下降沿与有效数据的开始之间的VCLK周期数 */
#define     HOZVAL      (LCD_WIDTH-1)  /* LCD的水平宽度-1 */
#define     HFPD                       /* 水平后沿为有效数据的结束与HSYNC的上升沿之间的VCLK周期数 */


struct lcd_regs {
	unsigned long	lcdcon1;
	unsigned long	lcdcon2;
	unsigned long	lcdcon3;
	unsigned long	lcdcon4;
	unsigned long	lcdcon5;
    unsigned long	lcdsaddr1;
    unsigned long	lcdsaddr2;
    unsigned long	lcdsaddr3;
    unsigned long	redlut;
    unsigned long	greenlut;
    unsigned long	bluelut;
    unsigned long	reserved[9];
    unsigned long	dithmode;
    unsigned long	tpal;
    unsigned long	lcdintpnd;
    unsigned long	lcdsrcpnd;
    unsigned long	lcdintmsk;
    unsigned long	lpcsel;
};

static volatile struct lcd_regs* lcd_regs;

struct vir_fb_info {
    struct fb_info fb_info; /* 第一个参数变量必须是fb_info */ 
    struct platform_device *pdev;
    struct clk *clk;
    void __iomem *base;    /* registers */
    int enabled;
    unsigned ld_intf_width;
    struct regulator *reg_lcd;
    unsigned int pseudo_palette[16];
    int pwm_gpio;
};

#define to_vir_fb_host(x) (container_of(x, struct vir_fb_info, fb_info))
#define RED 0
#define GREEN 1
#define BLUE 2
#define TRANSP 3

static const struct fb_bitfield vir_rgb565[] = {
    [RED] = {
        .offset = 11,
        .length = 5,
    },
    [GREEN] = {
        .offset = 5,
        .length = 6,
    },
    [BLUE] = {
        .offset = 0,
        .length = 5,
    },
    [TRANSP] = {    /* no support for transparency */
        .length = 0,
    }
};

static const struct fb_bitfield vir_rgb888[] = {
    [RED] = {
        .offset = 16,
        .length = 8,
    },
    [GREEN] = {
        .offset = 8,
        .length = 8,
    },
    [BLUE] = {
        .offset = 0,
        .length = 8,
    },
    [TRANSP] = {    /* no support for transparency */
        .length = 0,
    }
};

static int vir_fb_check_var(struct fb_var_screeninfo *var,
        struct fb_info *fb_info)
{
    // struct vir_fb_info *host = to_vir_fb_host(fb_info);
    const struct fb_bitfield *rgb = NULL;

    var->xres_virtual = var->xres;

    var->yres_virtual = var->yres;

    switch (var->bits_per_pixel) {
        case 16:
            /* always expect RGB 565 */
            rgb = vir_rgb565;
        break;
        case 24:
        case 32:
            rgb = vir_rgb888;
        break;
        default:
            pr_err("Unsupported colour depth: %u\n", var->bits_per_pixel);
        return -EINVAL;
    }

    /*
     * Copy the RGB parameters for this display
     * from the machine specific parameters.
     */
    var->red    = rgb[RED];
    var->green  = rgb[GREEN];
    var->blue   = rgb[BLUE];
    var->transp = rgb[TRANSP];

    return 0;
}

static int vir_fb_set_par(struct fb_info *info)
{

    return 0;
}

static int vir_fb_blank(int blank_mode, struct fb_info *info)
{

    return 0;
}

static inline unsigned int chan_to_field(unsigned int chan,
					 struct fb_bitfield *bf)
{
	chan &= 0xffff;
	chan >>= 16 - bf->length;
	return chan << bf->offset;
}

// 调色板操作函数
static int vir_fb_setcolreg(unsigned regno, unsigned red, unsigned green, unsigned blue, unsigned transp, struct fb_info *fb_info)
{
	unsigned int val;
	int ret = -EINVAL;

	switch (fb_info->fix.visual) {
	case FB_VISUAL_TRUECOLOR:
		/*
		 * 12 or 16-bit True Colour.  We encode the RGB value
		 * according to the RGB bitfield information.
		 */
		//调色板数组不能大于15
		if (regno < 16) {
			u32 *pal = fb_info->pseudo_palette;

            /* 用red,green,blue三个颜色值构造出16色数据val */
			val  = chan_to_field(red, &fb_info->var.red);
			val |= chan_to_field(green, &fb_info->var.green);
			val |= chan_to_field(blue, &fb_info->var.blue);
            /* 放到调色板数组中 */
			pal[regno] = val;
			ret = 0;
		}
		break;

	case FB_VISUAL_STATIC_PSEUDOCOLOR:
	case FB_VISUAL_PSEUDOCOLOR:
		break;
	}

	return ret;

}

static int vir_fb_pan_display(struct fb_var_screeninfo *var,
        struct fb_info *fb_info)
{ 
    // struct vir_fb_info *host = to_vir_fb_host(fb_info);
	unsigned offset;

	if (var->xoffset != 0)
		return -EINVAL;

	offset = fb_info->fix.line_length * var->yoffset;

    // APP多buff操作, 硬件操作
    
#if 0
	vir_fb_enable_clk(host);

	/* update on next VSYNC */
	writel(fb_info->fix.smem_start + offset,
			host->base + host->devdata->next_buf);

	vir_fb_disable_clk(host);
#endif

    return 0;
}

static struct fb_ops vir_fb_ops = {
    .owner          = THIS_MODULE,
    .fb_check_var   = vir_fb_check_var,
    .fb_set_par     = vir_fb_set_par,
    .fb_blank       = vir_fb_blank,
    .fb_setcolreg   = vir_fb_setcolreg,   // 调色板操作函数 设置调色板fb_info-> pseudo_palette
    .fb_pan_display = vir_fb_pan_display,
    .fb_fillrect    = cfb_fillrect,       // 填充矩形  函数在drivers/video/fbdev/core/cfbfillrect.c中定义
    .fb_copyarea    = cfb_copyarea,       // 复制数据  函数在drivers/video/fbdev/core/cfbcopyarea.c中定义
    .fb_imageblit   = cfb_imageblit,      // 绘制图形  函数在drivers/video/fbdev/core/cfbimgblt.c中定义
};

static int vir_fb_init_fbinfo_dt(struct vir_fb_info *host, struct fb_videomode *vmode)
{
    struct fb_info *fb_info = &host->fb_info;
    struct fb_var_screeninfo *var = &fb_info->var;
    struct device *dev = &host->pdev->dev;
    struct device_node *np = host->pdev->dev.of_node;
    struct device_node *display_np;
    struct videomode vm;
    unsigned int width = 0;
    int ret = 0;

    display_np = of_parse_phandle(np, "display", 0);
    if (!display_np) {
        dev_err(dev, "failed to find display phandle\n");
        return -ENOENT;
    }

    ret = of_property_read_u32(display_np, "bus-width", &width);
    if (ret < 0) {
        dev_err(dev, "failed to get property bus-width\n");
        goto put_display_node;
    }

    switch (width) {
    case 8:
        host->ld_intf_width = 8;
        break;
    case 16:
        host->ld_intf_width = 16;
        break;
    case 24:
    case 32:
        host->ld_intf_width = 24;
        break;
    default:
        dev_err(dev, "invalid bus-width value\n");
        ret = -EINVAL;
        goto put_display_node;
    }

    ret = of_property_read_u32(display_np, "bits-per-pixel",
                   &var->bits_per_pixel);
    if (ret < 0) {
        dev_err(dev, "failed to get property bits-per-pixel\n");
        goto put_display_node;
    }

    ret = of_get_videomode(display_np, &vm, OF_USE_NATIVE_MODE);
    if (ret) {
        dev_err(dev, "failed to get videomode from DT\n");
        goto put_display_node;
    }

    ret = fb_videomode_from_videomode(&vm, vmode);
    if (ret < 0)
        goto put_display_node;

put_display_node:
    of_node_put(display_np);
    return ret;
}

static int vir_fb_restore_mode(struct vir_fb_info *host,
            struct fb_videomode *vmode)
{
    struct fb_info *fb_info = &host->fb_info;
	unsigned int line_count;
	int bits_per_pixel, ret = 0;

    dev_err(NULL, "host->ld_intf_width %d\n", host->ld_intf_width);

	switch (host->ld_intf_width) {
	case 8:
		bits_per_pixel = 8;
		break;
	case 16:
		bits_per_pixel = 16;
		break;
    case 24:
    case 32:
        bits_per_pixel = 24;
    	break;
	default:
		ret = -EINVAL;
		goto err;
	}

	fb_info->var.bits_per_pixel = bits_per_pixel;

    // LCD一行所占字节数
    if (bits_per_pixel != 24)
	    fb_info->fix.line_length = vmode->xres * (bits_per_pixel >> 3);
    else
        fb_info->fix.line_length = vmode->xres * (32 >> 3);
	line_count = fb_info->fix.smem_len / fb_info->fix.line_length;

    dev_err(NULL, "fb_info->fix.line_length = %d line_count = %d\n", fb_info->fix.line_length, line_count);

err:
	return ret;
}

static int vir_fb_init_fbinfo(struct vir_fb_info *host, struct fb_videomode *vmode)
{
    int ret;
    struct device *dev = &host->pdev->dev;
    struct fb_info *fb_info = &host->fb_info;
    struct fb_var_screeninfo *var = &fb_info->var;
    dma_addr_t fb_phys;
    void *fb_virt;
    unsigned fb_size;

    /* 设置操作函数 */
    fb_info->fbops = &vir_fb_ops;
    fb_info->flags = FBINFO_FLAG_DEFAULT;

    /* 设置LCD固定的参数 */
    strlcpy(fb_info->fix.id, "vir_fb", sizeof(fb_info->fix.id));
    fb_info->fix.type = FB_TYPE_PACKED_PIXELS;
    fb_info->fix.type_aux        = 0;
    fb_info->fix.ypanstep        = 0;
    fb_info->fix.xpanstep        = 0;
    fb_info->fix.ywrapstep       = 0;
    fb_info->fix.visual          = FB_VISUAL_TRUECOLOR,   //真彩色
    fb_info->fix.accel           = FB_ACCEL_NONE;         // 使设置的值立即生效
    
    /* 从设备树中获取屏幕信息 */
    ret = vir_fb_init_fbinfo_dt(host, vmode);
    if (ret)
        return ret;

    /* 设置部分可变的参数 */
    var->nonstd = 0;
    var->activate = FB_ACTIVATE_NOW;
    var->accel_flags = 0;
    var->vmode = FB_VMODE_NONINTERLACED;

    /* 分配显存(framebuffer) */
    /* Memory allocation for framebuffer */
    //fb_size = SZ_2M;
    fb_size = vmode->xres * vmode->yres * host->ld_intf_width / 8;
    fb_virt = dma_alloc_wc(dev, PAGE_ALIGN(fb_size), &fb_phys, GFP_KERNEL);
    if (!fb_virt) {
        dev_err(NULL, "dma_alloc_wc failed\n");
        return -ENOMEM;
    }

    /* fb的物理地址/fb的虚拟地址 */
    fb_info->fix.smem_start = fb_phys;
    fb_info->screen_base = fb_virt;
    /* 设置显存的屏幕的内存大小,可用于单buffer和多buffer的LCD APP程序 */
    fb_info->screen_size = fb_info->fix.smem_len = fb_size; // framebuffer缓冲区的大小

    /* 把FB参数传递给fb_video_mode,设置屏幕的fix相关参数 */
    /* 根据LCD的特性的不同要求初始化 */
    if (vir_fb_restore_mode(host, vmode))
        memset(fb_virt, 0, fb_size); //清空现存数据

    return 0;
}

static const struct of_device_id vir_fb_lcd_dev_id[] = {
    { .compatible = "jz2440,virtual_fb_lcd_dev", },
    {},
};
MODULE_DEVICE_TABLE(of, vir_fb_lcd_dev_id);

static int vir_fb_lcd_dev_probe(struct platform_device *pdev)
{
    int ret = 0;
    struct device_node *display_np = NULL;
    struct device_node *np = NULL;
    struct device *dev = &pdev->dev;
    struct vir_fb_info *host = NULL;
    struct fb_info *fb_info = NULL;
    struct fb_videomode *mode = NULL;
    struct resource *res = NULL;
    int enable_gpio = 0;
    int rst_gpio = 0;
    int pwm_gpio = 0;

    np = dev->of_node;
    if (!np) {
        dev_err(dev, "could not find device info\n");
        return -EINVAL;
    }

#if 0 // 设备树中有GPIO控制
    enable_gpio = of_get_named_gpio(pdev->dev.of_node, "enable-gpio", 0);
	if (gpio == -EPROBE_DEFER)
		return -EPROBE_DEFER;

	if (gpio_is_valid(gpio)) {
		ret = devm_gpio_request_one(&pdev->dev, gpio, GPIOF_OUT_INIT_LOW, "lcd_pwr_en");
		if (ret) {
			dev_err(&pdev->dev, "faild to request gpio %d, ret = %d\n", gpio, ret);
			return ret;
		}
	}

    rst_gpio = of_get_named_gpio(pdev->dev.of_node, "reset-gpios", 0);
    if (gpio_is_valid(rst_gpio)) {
            ret = gpio_request(rst_gpio, "lcdif_rst");
            if (ret < 0) {
                dev_err(&pdev->dev,
                        "Failed to request GPIO:%d, ERRNO:%d\n",
                        (s32)rst_gpio, ret);
            } else {
                gpio_direction_output(rst_gpio, 0);
                msleep(2);
                gpio_direction_output(rst_gpio, 1);
                dev_info(&pdev->dev,  "Success seset LCDIF\n");
            }
    }
#endif

    // 屏幕的背光GPIO,不使用PWM,直接IO控制
    pwm_gpio = of_get_named_gpio(pdev->dev.of_node, "pwm-gpios", 0);
    if (gpio_is_valid(pwm_gpio)) {
        ret = gpio_request(pwm_gpio, "lcdif_pwm");
        if (ret < 0) {
            dev_err(&pdev->dev, "Failed to request GPIO:%d, ERRNO:%d\n", (s32)pwm_gpio, ret);
        }
    }    

    /* 分配fb_info */
    // 分配一个fb_info + vir_fb_info大小的fb_info
    fb_info = framebuffer_alloc(sizeof(struct vir_fb_info), &pdev->dev);
    if (!fb_info) {
        dev_err(&pdev->dev, "Failed to allocate fbdev\n");
        return -ENOMEM;
    }

    // 分配一个fb_videomode
    mode = devm_kzalloc(&pdev->dev, sizeof(struct fb_videomode), GFP_KERNEL);
    if (mode == NULL)
        return -ENOMEM;

    // 把fb_info的指针指向vir_fb_info
    /*   内存空间
     *  
     *   ------------
     *    fb_info1
     *   ------------
     *    vir_fb_info(fb_info2) 第一个成员可以强转成首地址 vir_fb_info* = (fb_info *)
     *
     *   container_of计算的是相对于的地址,使用fb_info1的地址计算,相当于fb_info1是vir_fb_info
     *   的成员变量,获取到的vir_fb_info包含了fb_info1 + sizeof(vir_fb_info)大小的空间,导致
     *   真实的vir_fb_info物理空间大于vir_fb_info指针执行的空间,多了sizeof(fb_info2)大小
     *
    */
    host = to_vir_fb_host(fb_info);

    /* fb设备树信息--->fb_videomode信息-->var_fb信息     */

    /* 获取display信息 */
    display_np = of_parse_phandle(pdev->dev.of_node, "display", 0);
    if (!display_np) {
        dev_err(&pdev->dev, "No display defined in devicetree\n");
        return -EINVAL;
    }

    /* 获取内存资源 */
    res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
    host->base = devm_ioremap_resource(&pdev->dev, res);
    if (IS_ERR(host->base)) {
        ret = PTR_ERR(host->base);
        goto fb_release;
    }

    /* 保存内存资源 */
    host->pdev = pdev;
    platform_set_drvdata(pdev, host);
    host->pwm_gpio = pwm_gpio;

    /* 获取时钟 */
    host->clk = devm_clk_get(&host->pdev->dev, "lcd");
    if (IS_ERR(host->clk)) {
        dev_err(&pdev->dev, "failed to get lcd clock source\n");
        ret = PTR_ERR(host->clk);
        goto fb_release;
    }


    /* 使能时钟 */
    clk_prepare_enable(host->clk);

    dev_err(NULL, "got and enabled clock\n");

    usleep_range(1000, 1100);

    /* 设置调色板 */
    fb_info->pseudo_palette = &host->pseudo_palette; // 保存调色板数组


    /* 获取设备树信息,存入fb_video_mode */
    ret = vir_fb_init_fbinfo(host, mode);
    if (ret != 0)
        goto fb_release;

    /* 把fb_video_mode的LCD信息转给可变参数 */
    /* 设置fb_info */
    fb_videomode_to_var(&fb_info->var, mode);

    /* init the color fields */
    /* 设置LCD的rgb的位宽和red/green/blue的偏移 */
    vir_fb_check_var(&fb_info->var, fb_info);

    platform_set_drvdata(pdev, fb_info);

    dev_err(NULL, "fix.id %s\n", fb_info->fix.id);
    dev_err(NULL, "fix.smem_len %d\n", fb_info->fix.smem_len);
    dev_err(NULL, "fix.type %d\n", fb_info->fix.type);
    dev_err(NULL, "fix.visual %d\n", fb_info->fix.visual);
    dev_err(NULL, "fix.line_length %d\n", fb_info->fix.line_length);
    
    dev_err(NULL, "var.xres %d\n", fb_info->var.xres); // 行分辨率
    dev_err(NULL, "var.yres %d\n", fb_info->var.yres); // 列分辨率
    dev_err(NULL, "var.xres_virtual %d\n", fb_info->var.xres_virtual); // 行虚拟分辨率
    dev_err(NULL, "var.yres_virtual %d\n", fb_info->var.yres_virtual); // 列虚拟分辨率
    dev_err(NULL, "var.bits_per_pixel %d\n", fb_info->var.bits_per_pixel); // 每像素使用位数
    dev_err(NULL, "var.xoffset %d\n", fb_info->var.xoffset); //虚拟到可见屏幕之间的行偏移
    dev_err(NULL, "var.yoffset %d\n", fb_info->var.yoffset); //虚拟到可见屏幕之间的行偏移
    
    dev_err(NULL, "var.red.offset %d\n", fb_info->var.red.offset);
    dev_err(NULL, "var.red.length %d\n", fb_info->var.red.length);
    dev_err(NULL, "var.green.offset %d\n", fb_info->var.green.offset);
    dev_err(NULL, "var.green.length %d\n", fb_info->var.green.length);
    dev_err(NULL, "var.blue.offset %d\n", fb_info->var.blue.offset);
    dev_err(NULL, "var.blue.length %d\n", fb_info->var.blue.length);
    dev_err(NULL, "var.activate %d\n", fb_info->var.activate);
    dev_err(NULL, "screen_size %ld\n", fb_info->screen_size);

    /* LCD硬件设置 */
    /* pinctrl配置GPIO用于LCD */
    /* 配置GPIO背光 */
	
	/* 根据LCD手册设置LCD控制器, 比如VCLK的频率等 */
	lcd_regs = ioremap(0x4D000000, sizeof(struct lcd_regs));
 
	/* bit[17:8]: VCLK = HCLK / [(CLKVAL+1) x 2], LCD手册P14
	 *            10MHz(100ns) = 100MHz / [(CLKVAL+1) x 2] //dmesg
	 *            CLKVAL = 4
	 * bit[6:5]: 0b11, TFT LCD
	 * bit[4:1]: 0b1100, 16 bpp for TFT
	 * bit[0]  : 0 = Disable the video output and the LCD control signal.
	 */
	lcd_regs->lcdcon1  = (4<<8) | (3<<5) | (0x0c<<1);
 
	/* 垂直方向的时间参数
	 * bit[31:24]: VBPD, VSYNC之后再过多长时间才能发出第1行数据
	 *             LCD手册 T0-T2-T1=4
	 *             VBPD=3
	 * bit[23:14]: 多少行, 320, 所以LINEVAL=320-1=319
	 * bit[13:6] : VFPD, 发出最后一行数据之后，再过多长时间才发出VSYNC
	 *             LCD手册T2-T5=322-320=2, 所以VFPD=2-1=1
	 * bit[5:0]  : VSPW, VSYNC信号的脉冲宽度, LCD手册T1=1, 所以VSPW=1-1=0
	 */
	 
	lcd_regs->lcdcon2  = (1<<24) | (271<<14) | (1<<6) | (9<<0);
 
	/* 水平方向的时间参数
	 * bit[25:19]: HBPD, VSYNC之后再过多长时间才能发出第1行数据
	 *             LCD手册 T6-T7-T8=17
	 *             HBPD=16
	 * bit[18:8]: 多少列, 240, 所以HOZVAL=240-1=239
	 * bit[7:0] : HFPD, 发出最后一行里最后一个象素数据之后，再过多长时间才发出HSYNC
	 *             LCD手册T8-T11=251-240=11, 所以HFPD=11-1=10
	 */
	lcd_regs->lcdcon3 = (1<<19) | (479<<8) | (1<<0);
	/* 水平方向的同步信号
	 * bit[7:0]	: HSPW, HSYNC信号的脉冲宽度, LCD手册T7=5, 所以HSPW=5-1=4
	 */	
	lcd_regs->lcdcon4 = 40;
 
	/* 信号的极性 
	 * bit[11]: 1=565 format
	 * bit[10]: 0 = The video data is fetched at VCLK falling edge
	 * bit[9] : 1 = HSYNC信号要反转,即低电平有效 
	 * bit[8] : 1 = VSYNC信号要反转,即低电平有效 
	 * bit[6] : 0 = VDEN不用反转
	 * bit[3] : 0 = PWREN输出0
	 * bit[1] : 0 = BSWP
	 * bit[0] : 1 = HWSWP 2440手册P413
	 */
	lcd_regs->lcdcon5 = (1<<11) | (0<<10) | (1<<9) | (1<<8) | (1<<0);
	
	/* 分配显存(framebuffer), 并把地址告诉LCD控制器 */
	fb_info->screen_base = dma_alloc_writecombine(NULL, fb_info->fix.smem_len, &fb_info->fix.smem_start, GFP_KERNEL);
	
	lcd_regs->lcdsaddr1  = (fb_info->fix.smem_start >> 1) & ~(3<<30);
	lcd_regs->lcdsaddr2  = ((fb_info->fix.smem_start + fb_info->fix.smem_len) >> 1) & 0x1fffff;
	lcd_regs->lcdsaddr3  = (480*16/16);  /* 一行的长度(单位: 2字节) */	
	
	//s3c_lcd->fix.smem_start = xxx;  /* 显存的物理地址 */
	/* 启动LCD */
	lcd_regs->lcdcon1 |= (1<<0); /* 使能LCD控制器 */
	lcd_regs->lcdcon5 |= (1<<3); /* 使能LCD本身 */
	/* 输出高电平, 使能背光 */
    gpio_direction_output(pwm_gpio, 1);

    ret = register_framebuffer(fb_info);
    if (ret != 0) {
        dev_err(&pdev->dev,"Failed to register framebuffer\n");
        goto fb_destroy;
    }

    dev_err(NULL, "register_framebuffer ret %d\n", ret);

    //vir_fb_enable_lcd_controller(fb_info);

    return 0;
    
fb_destroy:
    if (host->enabled)
        clk_disable_unprepare(host->clk);
    
fb_release:
    framebuffer_release(fb_info);

    return ret;
}

static int vir_fb_lcd_dev_remove(struct platform_device *pdev)
{
    struct fb_info *fb_info = platform_get_drvdata(pdev);
    struct vir_fb_info *host = to_vir_fb_host(fb_info);
    struct device *dev = &host->pdev->dev;

    lcd_regs->lcdcon1 &= ~(1<<0); /* 关闭LCD本身 */
    /* 关闭背光 */
    gpio_set_value(host->pwm_gpio, 0);
    gpio_free(host->pwm_gpio);
    
    unregister_framebuffer(fb_info);
    dma_free_wc(dev, fb_info->screen_size, fb_info->screen_base, fb_info->fix.smem_start);
    if (host->clk) {
		clk_disable_unprepare(host->clk);
		clk_put(host->clk);
		host->clk = NULL;
	}
    framebuffer_release(fb_info);
    
    return 0;
}

static struct platform_driver vir_fb_lcd_dev_driver = {
    .probe = vir_fb_lcd_dev_probe,
    .remove = vir_fb_lcd_dev_remove,
    .driver = {
        .name = "jz2440-vir-fb-lcd-dev",
        .of_match_table = of_match_ptr(vir_fb_lcd_dev_id),
    }
};


/*
 * init入口函数
*/
static int vir_fb_lcd_drv_init(void)
{
    int ret = platform_driver_register(&vir_fb_lcd_dev_driver);
    if (ret < 0) {
        dev_err(NULL, "vir_fb_lcd_drv_init failed\n");
        return -EBUSY;
    }
    
    return ret;
}

/*
 * exit出口函数
 */
static void vir_fb_lcd_drv_exit(void)
{
    platform_driver_unregister(&vir_fb_lcd_dev_driver);
    return;
}

module_init(vir_fb_lcd_drv_init);
module_exit(vir_fb_lcd_drv_exit);


MODULE_AUTHOR("huang");
MODULE_LICENSE("GPL");
MODULE_DESCRIPTION("virtual fb lcd driver");


